      * GRUPO XX DE ALGORITMOS Y PROGRAMACION IV (7527).
      * FACULTAD DE INGENIERIA DE LA UNIVERSIDAD DE BUENOS AIRES.

      * ARCHIVO PARA TODAS LAS DEFINICIONES

	IDENTIFICATION DIVISION.
	PROGRAM-ID. TP-ETAPA1.
	AUTHOR. BASUALDO - CUEVAS - MONTES - ROMERO.

      * --------------------------------------------------------------- *

	ENVIRONMENT DIVISION.
	CONFIGURATION SECTION.
	SPECIAL-NAMES.
	    DECIMAL-POINT IS COMMA.

      * --------------------------------------------------------------- *

	INPUT-OUTPUT SECTION.
	FILE-CONTROL.

            SELECT RESERVAS-1          ASSIGN TO 'RESERVAS-1.DAT'
                                        FILE STATUS IS WSS-FS-RES1.

            SELECT RESERVAS-2          ASSIGN TO 'RESERVAS-2.DAT'
                                        FILE STATUS IS WSS-FS-RES2.

            SELECT RESERVAS-3          ASSIGN TO 'RESERVAS-3.DAT'
                                        FILE STATUS IS WSS-FS-RES3.

            SELECT RESERVAS-VIP        ASSIGN TO 'VIP.DAT'
                                        FILE STATUS IS WSS-FS-VIP.

            SELECT ESTADIAS             ASSIGN TO 'ESTADIAS.DAT'
                                        FILE STATUS IS WSS-FS-EST.

            SELECT CATEGORIAS          ASSIGN TO 'CATEGORIAS.DAT'
                                        FILE STATUS IS WSS-FS-CAT.

            SELECT DESCUENTOS          ASSIGN TO 'DESCUENTOS.DAT'
                                        FILE STATUS IS WSS-FS-DESC.
	
            SELECT MAESTRO              ASSIGN TO 'MAESTRO.DAT'
                                        FILE STATUS IS WSS-FS-MAESTRO.
      * --------------------------------------------------------------- *

	DATA DIVISION.
	FILE SECTION.
	
        FD RESERVAS-1           DATA RECORD IS RES1
				LABEL RECORD IS STANDARD.

        01 RES1                 PIC X(120).

      * ***************************************

        FD RESERVAS-2           LABEL RECORD IS STANDARD
                                DATA RECORD IS RES2.

        01 RES2                 PIC X(120).
      * ***************************************

        FD RESERVAS-3           LABEL RECORD IS STANDARD
                                DATA RECORD IS RES3.

        01 RES3                 PIC X(120).
      * ***************************************

        FD RESERVAS-VIP         LABEL RECORD IS STANDARD
                                DATA RECORD IS VIP.

        01 VIP                  PIC X(120).
      * ***************************************
	FD ESTADIAS		DATA RECORD IS EST
				LABEL RECORD IS STANDARD.

        01 EST                  PIC X(40).

      * *****************************************

	FD CATEGORIAS		DATA RECORD IS CAT
				LABEL RECORD IS STANDARD.

        01 CAT                  PIC X(19).

      * ******************************************
	   
	FD DESCUENTOS		DATA RECORD IS DESC
				LABEL RECORD IS STANDARD.

        01 DESC                 PIC X(10).

      * ******************************************	   

	FD MAESTRO		DATA RECORD IS MAE
				LABEL RECORD IS STANDARD.

        01 MAE                  PIC X(121).

      * --------------------------------------------------------------- *

	WORKING-STORAGE SECTION.

      * ------------------------- CONSTANTES ------------------------- *


      * ------------------------- VARIABLES -------------------------- *
        01 TABLA-CATEGORIAS.
           05 WSV-TABLA-CATEGORIAS              OCCURS 10.
              10  WSV-TBL-CAT-HB                PIC 9(4).
              10  WSV-TBL-CAT-DESC              PIC X(15).

        01 WSV-TBL-HLD.
           05  WSV-CAT-HB-HLD                   PIC 9(4).
           05  WSV-CAT-DESC-HLD                 PIC X(15).


      * ------------------------- INDICES ---------------------------- *

        01  WSI-I                               PIC 9(2) VALUE 1.
        01  WSI-J                               PIC 9(2) VALUE 1.
        01  WSI-SUB                             PIC 9(2) VALUE 1.

        77 WS-DESC                        PIC S9(06)V9(02).
      * ------------------------- SWHITCHES -------------------------- *
        01 SWITCHES.
           03 WSS-FS-RES1                 PIC X(2).

           03 WSS-FS-RES2                 PIC X(2).

           03 WSS-FS-RES3                 PIC X(2).

           03 WSS-FS-VIP                  PIC X(2).

           03 WSS-FS-EST                  PIC X(2).

           03 WSS-FS-CAT                  PIC X(2).

           03 WSS-FS-DESC                 PIC X(2).

           03 WSS-FS-MAESTRO              PIC X(2).

           03 WSS-FS-EOF-RES1             PIC X(2)     VALUE 'NO'.
           03 WSS-FS-EOF-RES2             PIC X(2)     VALUE 'NO'.
           03 WSS-FS-EOF-RES3             PIC X(2)     VALUE 'NO'.
           03 WSS-FS-EOF-VIP              PIC X(2)     VALUE 'NO'.
           03 WSS-FS-EOF-EST              PIC X(2)     VALUE 'NO'.
           03 WSS-FS-EOF-CAT              PIC X(2)     VALUE 'NO'.
           03 WSS-FS-EOF-DESC             PIC X(2)     VALUE 'NO'.

      * ---------------------------------

        01 WS-MENOR.
           03 WS-MENOR-COD-RES            PIC 9(6).
           03 WS-MENOR-DIGITO             PIC 9.


      * ------------------------- REGISTROS -------------------------- *

        01 WS-REG-RES.
           03 WS-REG-RES-CLAVE.
              05 WS-REG-RES-COD-RES       PIC 9(6).
              05 WS-REG-RES-DIGITO        PIC 9.
           03 WS-REG-RES-FECHA-DES.
              05 WS-REG-RES-ANIO-DES      PIC X(4).
              05 FILLER                   PIC X VALUE '-'.
              05 WS-REG-RES-MES-DES       PIC X(2).
              05 FILLER                   PIC X VALUE '-'.
              05 WS-REG-RES-DIA-DES       PIC X(2).
           03 WS-REG-RES-FECHA-HAS.
              05 WS-REG-RES-ANIO-HAS      PIC X(4).
              05 FILLER                   PIC X VALUE '-'.
              05 WS-REG-RES-MES-HAS       PIC X(2).
              05 FILLER                   PIC X VALUE '-'.
              05 WS-REG-RES-DIA-HAS       PIC X(2).
           03 WS-REG-RES-CATEG-HAB        PIC 9(4).
           03 WS-REG-RES-DATOS-PERS.
              05 WS-REG-RES-NOMBRE-CLI    PIC X(30).
              05 WS-REG-RES-TEL           PIC X(15).
              05 WS-REG-RES-DIR           PIC X(30).
           03 WS-REG-RES-IMP-RES          PIC S9(06)V99.

      * -----------------------

        01 WS-REG-EST.
           03 WS-REG-EST-CLAVE.
              05 WS-REG-EST-COD-RES       PIC 9(6).
              05 WS-REG-EST-DIGITO        PIC 9.
           03 WS-REG-EST-FECHA-DES.
              05 WS-REG-EST-ANIO-DES      PIC X(4).
              05 FILLER                   PIC X VALUE '-'.
              05 WS-REG-EST-MES-DES       PIC X(2).
              05 FILLER                   PIC X VALUE '-'.
              05 WS-REG-EST-DIA-DES       PIC X(2).
           03 WS-REG-EST-FECHA-HAS.
              05 WS-REG-EST-ANIO-HAS      PIC X(4).
              05 FILLER                   PIC X VALUE '-'.
              05 WS-REG-EST-MES-HAS       PIC X(2).
              05 FILLER                   PIC X VALUE '-'.
              05 WS-REG-EST-DIA-HAS       PIC X(2).
           03 WS-REG-EST-CATEG-HAB        PIC 9(4).
           03 WS-REG-EST-IMPORTE          PIC S9(06)V9(2).

      * -----------------------

        01 WS-REG-DESC.
           03 WS-REG-DESC-CLAVE.
              05 WS-REG-DESC-COD-RES      PIC 9(6).
              05 WS-REG-DESC-DIG          PIC 9.
           03 WS-REG-DESC-PORC            PIC 9(3)V9(2).

      * -----------------------

        01 WS-REG-MAE.
	   03 WS-REG-MAE-CLAVE.
	      05 WS-REG-MAE-COD-RES       PIC 9(6).
	      05 WS-REG-MAE-DIGITO        PIC 9.
           03 WS-REG-MAE-FECHA-DES.
              05 WS-REG-MAE-ANIO-DES      PIC X(4).
              05 FILLER                   PIC X VALUE '-'.
              05 WS-REG-MAE-MES-DES       PIC X(2).
              05 FILLER                   PIC X VALUE '-'.
              05 WS-REG-MAE-DIA-DES       PIC X(2).
           03 WS-REG-MAE-FECHA-HAS.
              05 WS-REG-MAE-ANIO-HAS      PIC X(4).
              05 FILLER                   PIC X VALUE '-'.
              05 WS-REG-MAE-MES-HAS       PIC X(2).
              05 FILLER                   PIC X VALUE '-'.
              05 WS-REG-MAE-DIA-HAS       PIC X(2).
	   03 WS-REG-MAE-CATEG-HAB        PIC 9(4).
	   03 WS-REG-MAE-DATOS-PERS.
	      05 WS-REG-MAE-NOMBRE-CLI    PIC X(30).
	      05 WS-REG-MAE-TEL           PIC X(15).
	      05 WS-REG-MAE-DIR           PIC X(30).
           03 WS-REG-MAE-IMP              PIC S9(06)V99.
           03 WS-REG-MAE-ESTADO           PIC X.

      * --------------------------

        01 WS-REG-CAT.
           03 WS-REG-CAT-HAB              PIC 9(4).
           03 WS-REG-CAT-DESCRIP          PIC X(15).

      * -------------------------------------------------------------- *

		
        PROCEDURE DIVISION.
	GENERAL.
	
	    PERFORM INICIO.
	    PERFORM LECTURA-INICIAL.
	    PERFORM CARGA-Y-ORDENA-TAB-CAT.
            PERFORM PROCESO UNTIL WSS-FS-EOF-VIP = 'SI'
                                AND WSS-FS-EOF-RES1 = 'SI'
                                AND WSS-FS-EOF-RES2 = 'SI'
                                AND WSS-FS-EOF-RES3 = 'SI'.
	    PERFORM FIN.
	    STOP RUN.
	
	
      * ***************************  INICIO  ***************************
	INICIO.
           OPEN INPUT RESERVAS-1.
           IF WSS-FS-RES1 NOT = ZERO
              DISPLAY 'ERROR EN OPEN, FS: ' WSS-FS-RES1
              STOP RUN.

           OPEN INPUT RESERVAS-2.
           IF WSS-FS-RES2 NOT = ZERO
              DISPLAY 'ERROR EN OPEN, FS: ' WSS-FS-RES2
              STOP RUN.

           OPEN INPUT RESERVAS-3.
           IF WSS-FS-RES3 NOT = ZERO
              DISPLAY 'ERROR EN OPEN, FS: ' WSS-FS-RES3
              STOP RUN.

           OPEN INPUT RESERVAS-VIP.
           IF WSS-FS-VIP NOT = ZERO
              DISPLAY 'ERROR EN OPEN, FS: ' WSS-FS-VIP
              STOP RUN.

           OPEN INPUT ESTADIAS.
           IF WSS-FS-EST NOT = ZERO
              DISPLAY 'ERROR EN OPEN, FS: ' WSS-FS-EST
              STOP RUN.

           OPEN INPUT CATEGORIAS.
           IF WSS-FS-CAT NOT = ZERO
              DISPLAY 'ERROR EN OPEN, FS: ' WSS-FS-CAT
              STOP RUN.


           OPEN OUTPUT MAESTRO.

      * *************************  LECTURA-INICIAL  ********************
	LECTURA-INICIAL.
            PERFORM 421000-LEO-RES1.
            PERFORM 422000-LEO-RES2.
            PERFORM 423000-LEO-RES3.
            PERFORM 424000-LEO-VIP.
            PERFORM 425000-LEO-EST.
            PERFORM 426000-LEO-CAT.
            PERFORM 427000-LEO-DESC.
	
      * *************************** LEO-RES1 ***************************
        421000-LEO-RES1.
           READ RESERVAS-1
              AT END MOVE 'SI' TO WSS-FS-EOF-RES1.

           IF WSS-FS-RES1 NOT = ZERO AND 10
              DISPLAY 'ERROR EN READ FS: ' WSS-FS-RES1
              STOP RUN.
      * *************************** LEO-RES2 ***************************
        422000-LEO-RES2.
           READ RESERVAS-2
           AT END MOVE 'SI' TO WSS-FS-EOF-RES2.

           IF WSS-FS-RES2 NOT = ZERO AND 10
              DISPLAY 'ERROR EN READ FS: ' WSS-FS-RES2
              STOP RUN.
      * *************************** LEO-RES3 ***************************
        423000-LEO-RES3.
           READ RESERVAS-3
           AT END MOVE 'SI' TO WSS-FS-EOF-RES3.

           IF WSS-FS-RES3 NOT = ZERO AND 10
              DISPLAY 'ERROR EN READ FS: ' WSS-FS-RES3
              STOP RUN.

      * *************************** LEO-VIP ****************************
        424000-LEO-VIP.
           READ RESERVAS-VIP
           AT END MOVE 'SI' TO WSS-FS-EOF-VIP.

           IF WSS-FS-VIP NOT = ZERO AND 10
              DISPLAY 'ERROR EN READ FS: ' WSS-FS-VIP
              STOP RUN.

      * *************************** LEO-EST ***************************
        425000-LEO-EST.
           READ ESTADIAS
           AT END MOVE 'SI' TO WSS-FS-EOF-EST.

           IF WSS-FS-EST NOT = ZERO AND 10
              DISPLAY 'ERROR EN READ FS: ' WSS-FS-EST
              STOP RUN.

      * **************************** LEO-CAT ***************************
        426000-LEO-CAT.
           READ CATEGORIAS
           AT END MOVE 'SI' TO WSS-FS-EOF-CAT.

           IF WSS-FS-CAT NOT = ZERO AND 10
              DISPLAY 'ERROR EN READ FS: ' WSS-FS-CAT
              STOP RUN.

      * **************************** LEO-DESC **************************
        427000-LEO-DESC.
           READ DESCUENTOS
           AT END MOVE 'SI' TO WSS-FS-EOF-DESC.

           IF WSS-FS-DESC NOT = ZERO AND 10
              DISPLAY 'ERROR EN READ FS: ' WSS-FS-DESC
              STOP RUN.

      * **********************  CARGA-Y-ORDENA-TAB-CAT *****************
	CARGA-Y-ORDENA-TAB-CAT.
           PERFORM 411000-ABRIR-ARCHIVO-CATEGORIAS.
           PERFORM 426000-LEO-CAT.
           PERFORM 500000-CARGAR-TABLA UNTIL WSS-FS-EOF-CAT = 'SI'
                                             OR WSI-SUB > 50.
           PERFORM 600000-ORDENAR-TABLA-CATEGORIAS.
           PERFORM 431000-CERRAR-ARCH-CATEGORIAS.

        411000-ABRIR-ARCHIVO-CATEGORIAS.
           OPEN INPUT CATEGORIAS.


        500000-CARGAR-TABLA.
           MOVE CAT TO WSV-TABLA-CATEGORIAS(WSI-SUB)
           ADD 1 TO WSI-SUB.
           PERFORM 426000-LEO-CAT.

        600000-ORDENAR-TABLA-CATEGORIAS.
           PERFORM VARYING WSI-I FROM 1 BY 1 UNTIL WSI-I = WSI-SUB + 1
              PERFORM VARYING WSI-J FROM WSI-I BY 1 UNTIL
                                                       WSI-J > WSI-SUB
                 IF WSV-TBL-CAT-HB(WSI-J) < WSV-TBL-CAT-HB(WSI-I) THEN
                    MOVE WSV-TABLA-CATEGORIAS(WSI-I)  TO WSV-TBL-HLD
                    MOVE WSV-TABLA-CATEGORIAS(WSI-J)  TO
                                            WSV-TABLA-CATEGORIAS(WSI-I)
                    MOVE WSV-TBL-HLD TO WSV-TABLA-CATEGORIAS(WSI-J)
                 END-IF
              END-PERFORM
           END-PERFORM.

        431000-CERRAR-ARCH-CATEGORIAS.
           CLOSE CATEGORIAS.


      * ****************************  FIN  *****************************
	FIN.
            CLOSE RESERVAS-1 RESERVAS-2 RESERVAS-3 RESERVAS-VIP.
            CLOSE ESTADIAS DESCUENTOS MAESTRO.
	
	
      * *************************** PROCESO ****************************
	PROCESO.
	    PERFORM DETERMINAR-MENOR.
	    PERFORM POSIBLE-RES-N.
	    PERFORM POSIBLE-RES-VIP.
	    PERFORM CHEQUEAR-ESTADIA.
	    PERFORM APLICAR-DESCUENTO.
	    PERFORM GRABAR-EN-MAESTRO.
	    PERFORM PROCESAR-REPORTE-ACEPTADA.
	    
      * *************************** DETERMINAR-MENOR *******************
	DETERMINAR-MENOR.
	
	
      * *************************** POSIBLE-RES-N **********************
	POSIBLE-RES-N.
	
	
      * ************************** POSIBLE-RES-VIP *********************
	POSIBLE-RES-VIP.
           IF WS-MENOR = WS-REG-RES-CLAVE
              PERFORM OBTENER-ULTIMA-MODIF
              MOVE 'N' TO WS-REG-MAE-ESTADO
              IF WSS-FS-EOF-VIP = 'SI'
                 MOVE 9999 TO WS-REG-RES-COD-RES
                 MOVE 9    TO WS-REG-RES-DIGITO.

      * ********************** OBTENER-ULTIMA-MODIF ********************
        OBTENER-ULTIMA-MODIF.

      * ************************** CHEQUEAR-ESTADIA ********************
	CHEQUEAR-ESTADIA.
	
	
      * ************************** APLICAR-DESCUENTO *******************
	APLICAR-DESCUENTO.
            IF WS-MENOR = WS-REG-DESC-CLAVE
               COMPUTE WS-DESC = (WS-REG-MAE-IMP * 100)/
               WS-REG-DESC-PORC
               SUBTRACT WS-DESC FROM WS-REG-MAE-IMP
               PERFORM 427000-LEO-DESC.
	
      * ************************** GRABAR-EN-MAESTRO *******************
	GRABAR-EN-MAESTRO.
	    MOVE WS-REG-MAE TO MAE.
	    WRITE MAE.
	
	
      * *********************** PROCESAR-REPORTE-ACEPTADA **************
	PROCESAR-REPORTE-ACEPTADA.
            IF WS-REG-MAE-ESTADO = 'A'
               PERFORM CARGAR-REPORTE-LISTADO
               PERFORM CARGAR-REPORTE-ESTADISTICAS.


      * *********************** CARGAR-REPORTE-LISTADO *****************
        CARGAR-REPORTE-LISTADO.


      * ********************* CARGAR-REPORTE-ESTADISTICAS **************
        CARGAR-REPORTE-ESTADISTICAS.
